﻿@using NPOI.SS.Formula.Functions
@model Matrix.MaKaYunShang.Model.Coupon
@{
    ViewBag.Title = "编辑优惠劵";
    ViewBag.PathToBtn = "/Coupon/List";
    ViewBag.TextBtn = "返回至列表";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<!-- /.row -->
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                优惠劵信息
            </div>
            <div class="panel-body">
                <div class="row">
                    @using (Html.BeginForm("Update", "Coupon", FormMethod.Post, new { id = "frmUpdate", enctype = "multipart/form-data" }))
                    {
                        @Html.AntiForgeryToken()
                        @Html.ValidationSummary(true)
                        <div class="col-lg-12">
                            <fieldset>
                                @Html.HiddenFor(model => model.Id)
                                <div class="form-group col-md-6">
                                    <label>卡卷名称</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.CouponName, new {@class = "form-control", placeholder = "卡卷名称"})
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>卡卷类别</label>
                                    <div class="input-group">
                                        @Html.DropDownList("CouponCategoryId", (IEnumerable<SelectListItem>)ViewBag.CouponCategory, new { @class = "form-control" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>库存总数量</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.TotalQuantity, new { @class = "form-control", placeholder = "库存总数量" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6 DiscountValue">
                                    <label>打折额度</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.DiscountValue, new { @class = "form-control", placeholder = "打折额度,百分比" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>卡卷价值</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.CardValue, new { @class = "form-control", placeholder = "卡卷价值" })
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div id="secSetCloseTime" class="form-group col-md-6" style=" ">
                                    <label>生效时间</label>
                                    <div class="input-group">
                                        @Html.TextBoxFor(model => model.StartTime, new { @class = "form-control form_datetime", placeholder = "开始", Value = String.Format("{0:yyyy-MM-dd HH:mm}", Model.StartTime), @onfocus = "WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd'})" })
                                        @Html.TextBoxFor(model => model.EndTime, new { @class = "form-control form_datetime", placeholder = "结束", Value = String.Format("{0:yyyy-MM-dd HH:mm}", Model.EndTime), @onfocus = "WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM-dd'})" })
                                 
                                        <span class="input-group-addon red">必填</span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>描述</label>
                                    <div class="input-group">
                                        @Html.TextAreaFor(model => model.Description, new { @class = "form-control", rows = "3", placeholder = "描述" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>使用说明</label>
                                    <div class="input-group">
                                        @Html.TextAreaFor(model => model.Instruction, new { @class = "form-control", rows = "3", placeholder = "使用说明" })
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>免责声明</label>
                                    <div class="input-group">
                                        @Html.TextAreaFor(model => model.Disclaimer, new {@class = "form-control", rows = "3", placeholder = "免责声明"})
                                        <span class="input-group-addon"></span>
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label>是否可与其他优惠券同时使用</label>
                                    <div class="input-group">
                                        @Html.CheckBoxFor(model => model.IsMultiUse)
                                    </div>
                                </div>
                                <div class="form-group col-md-10">
                                    <label><a data-target="#CommodityCategoryModel" data-toggle="modal" class="btn btn-info" onclick="transfer(19);">限制品类</a></label>
                                    <label id="CommodityCategoryText">@Model.CommodityCategoryText</label><a href="javascript:CommodityCategoryCar()" id="CommodityCategoryCar">清除</a>
                                    <input type="hidden" id="CommodityCategoryArr" name="CommodityCategoryArr" value="@Model.CommodityCategoryArr" />
                                    <div class="input-group">
                                    </div>
                                </div>
                                <div class="form-group col-md-10">
                                    <label><a data-target="#TransferOrderModal" data-toggle="modal" class="btn btn-info" onclick="transfer(19);">限制商品</a></label>
                                    <label id="CommodityLibText">@Model.CommodityLibText</label><a href="javascript:CommodityLibCar()" id="CommodityLibCar">清除</a>
                                    <input type="hidden" id="CommodityLibArr" name="CommodityLibArr" value="@Model.CommodityLibArr" />
                                    <div class="input-group">
                                    </div>
                                </div>

                                <div class="col-md-12">
                                    <button id="btnSave" type="submit" class="btn btn-primary">保存</button>
                                    <button id="btnReset" type="reset" class="btn btn-outline btn-primary">清空设置</button>
                                </div>
                            </fieldset>
                        </div>
                    }
                </div>
                <!-- /.row (nested) -->
            </div>
            <!-- /.panel-body -->
        </div>
        <!-- /.panel -->
    </div>
    <!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="modal fade" id="TransferOrderModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="max-width: 600px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">查询商品</h4>
            </div>
            <div class="modal-body">
                <div style="margin-bottom: 5px;">
                    <input type="text" class="form-control" placeholder="商品名称" value="冰川时代" style="width: 359px; display: inline-block" id="CommodityNameKeyWords" />
                    <a class="btn btn-primary" onclick="searchCommodityLib();">查询</a>
                    <a class="btn btn-info" style="width: 100px" onclick="SaveCommodityLib();">保存</a>
                </div>
                <div class="table-responsive table-bordered" style="max-height: 250px;">
                    <table class="table">
                        <thead>
                            <tr>
                                <th><input type="checkbox" name="check" onclick="check_all(this, 'check');" /></th>
                                <th>商品名称</th>
                                <th>单位</th>
                                <th>品牌</th>
                             
                            </tr>
                        </thead>
                        <tbody id="CommodityLibData"></tbody>
                    </table>
                    <input type="hidden" id="hidOrderId" />
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="CommodityCategoryModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="max-width: 600px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">查询品类</h4>
            </div>
            <div class="modal-body">
                <div style="margin-bottom: 5px;">
                    <input type="text" class="form-control" placeholder="品类名称" value="" style="width: 359px; display: inline-block" id="CommodityCategoryKeyWords" />
                    <a class="btn btn-primary" onclick="searchCommodityCategory();">查询</a>
                    <a class="btn btn-info" style=" width: 100px" onclick="SaveCommodityCategory();">保存</a>
                </div>
                <div class="table-responsive table-bordered" style="max-height: 250px;">
                    <table class="table">
                        <thead>
                            <tr>
                                <th><input type="checkbox" name="check2" onclick="check_all(this, 'check2');" /></th>
                                <th>品类名称</th>
                                <th>描述</th>

                            </tr>
                        </thead>
                        <tbody id="CommodityCategoryData"></tbody>
                    </table>
                    <input type="hidden" id="hidOrderId" />
                </div>
            </div>
        </div>
    </div>
</div>
@section javascript{
    <!-- Baidu Map JavaScript -->
    <script type="text/javascript">
        $(function () {
            $("#frmUpdate").validate({
                rules: {
                    CouponName: { required: true },
                    TotalQuantity: { required: true, digits: true },
                    DiscountValue: { required: true, number: true },
                    CardValue: { required: true, number: true },
                    StartTime: { required: true },
                    EndTime: { required: true }
                },
                messages: {
                    CouponName: { required: "请填写卡卷名称" },
                    TotalQuantity: { required: "请填写库存总数量" },
                    DiscountValue: { required: "请填写打折额度" },
                    CardValue: { required: "请填写卡卷价值" },
                    StartTime: { required: "请选择开始时间" },
                    EndTime: { required: "请选择结束时间" }
                },
                submitHandler: function (form) {
                    form.submit();
                }
            });
            if ($("#CouponCategoryId").val() == 2) {
              
                $(".DiscountValue").hide();
            } else {
       
                $(".DiscountValue").show();
            }
            $("#CouponCategoryId").change(function (t) {
                if ($("#CouponCategoryId").val() == 2) {
                    $("#DiscountValue").val(100);
                    $(".DiscountValue").hide();
                } else {
                    $("#DiscountValue").val(0);
                    $(".DiscountValue").show();
                }
            });

            $("#Disabled").change(function () {
                if ($("#Disabled").is(":checked")) {
                    $("#btnSave").html("保存");
                } else {
                    $("#btnSave").html("确认添加");
                }
            });

            $("#BusinessStatus").change(function () {
                if ($(this).val() == 1) {
                    $("#secSetCloseTime").hide();
                } else {
                    $("#secSetCloseTime").show();
                }
            });

            searchCommodityCategory();
            searchCommodityLib();
            if ($("#CommodityLibArr").val() == '') {
                $("#CommodityLibCar").hide();
            }
            if ($("#CommodityCategoryArr").val() == '') {
                $("#CommodityCategoryCar").hide();
            }
        });

        //搜索商品
        function searchCommodityLib() {
            $("input[name='check']").each(function () {
                $(this).attr("checked", false);
            });
            $("#CommodityLibData").html("");
            $.post("@Url.Action("GetCommodityLibList")", "&keyWords=" + encodeURIComponent($("#CommodityNameKeyWords").val()), function (data) {
                if (data.Status) {
                    for (var i = 0; i < data.Data.length; i++) {
                        $("#CommodityLibData").append("<tr><td><input type='checkbox' name='check' title=" + data.Data[i].CommodityName + " id='check' value=" + data.Data[i].Id + " onclick='setSelectAll();'/></td><td>" + data.Data[i].CommodityName + "</td><td>" + data.Data[i].Unit + "</td><td>" + data.Data[i].BrandName + "</td></tr>");
                    }
                } else {
                    alertify.alert(data.Message);
                }
            });
        }

        //保存商品
        function SaveCommodityLib() {
            var temp = document.getElementsByName('check');
            var idStr = "";
            var nameStr = "";
            for (var i = 1; i < temp.length; i++) {
                if (temp[i].checked == true) {
                    nameStr += temp[i].title + ",";
                    idStr += temp[i].value + ",";
                }
            }
            if (idStr == "") {
                return alertify.alert("请选择商品");
            }

            $("#CommodityLibText").html("(" + nameStr.substring(0, nameStr.length - 1) + ")");
            $("#CommodityLibArr").val(idStr);
            $("#CommodityLibCar").show();
            $("#TransferOrderModal").modal("hide");
        }

        function CommodityLibIdByBatchName() {
            $("#CommodityLibIdByBatchName").html("");
            $("#CommodityLibIdByBatch").val("");
            $("#QuantityOfBatchSale").val("");
            $("#CommodityLibIdByBatchNameCar").hide();
        }

        function CommodityLibCar() {
            $("#CommodityLibText").html("");
            $("#CommodityLibArr").val("");
            $("#CommodityLibCar").hide();
        }
        function check_all(obj, cName) {

            var checkboxs = document.getElementsByName(cName);
            for (var i = 0; i < checkboxs.length; i += 1) {
                checkboxs[i].checked = obj.checked;
            }
        }

        function searchCommodityCategory() {
      
            $("#CommodityCategoryData").html("");
            $.post("@Url.Action("GetCommodityCategoryList")", "&keyWords=" + encodeURIComponent($("#CommodityCategoryKeyWords").val()), function (data) {
                if (data.Status) {
                    for (var i = 0; i < data.Data.length; i++) {
                        $("#CommodityCategoryData").append("<tr><td><input type='checkbox' name='check2' title=" + data.Data[i].CategoryName + " id='check2' value=" + data.Data[i].Id + " onclick='setSelectAll();'/></td><td>" + data.Data[i].CategoryName + "</td><td>" + data.Data[i].Description + "</td></tr>");
                    }
                } else {
                    alertify.alert(data.Message);
                }
            });
        }

        //保存商品
        function SaveCommodityCategory() {
            var temp = document.getElementsByName('check2');
            var idStr2 = "";
            var nameStr2 = "";
            for (var i = 1; i < temp.length; i++) {
                if (temp[i].checked == true) {
                    nameStr2 += temp[i].title + ",";
                    idStr2 += temp[i].value + ",";
                }
            }
            if (idStr2 == "") {
                return alertify.alert("请选择品类");
            }
            $("#CommodityCategoryText").html("(" + nameStr2.substring(0, nameStr2.length - 1) + ")");
            $("#CommodityCategoryArr").val(idStr2);
            $("#CommodityCategoryCar").show();
            $("#CommodityCategoryModel").modal("hide");
        }
        function CommodityCategoryCar() {
            $("#CommodityCategoryText").html("");
            $("#CommodityCategoryArr").val("");
            $("#CommodityCategoryCar").hide();
        }

        $('#CommodityCategoryModel').on('shown.bs.modal', function () {
            // 执行一些动作...
            $("input[name='check2']").each(function () {
                $(this).attr("checked", false);
            });
        })
        $('#TransferOrderModal').on('shown.bs.modal', function () {
            // 执行一些动作...
            $("input[name='check']").each(function () {
                $(this).attr("checked", false);
            });
        })
    </script>
}